<?php
namespace Admin\Model;
use Think\Model;
class CourseInstructorModel extends Model{
    function success(){
        $result=[
            'error' => 0,
            'message' => '成功'
        ];
        return json_encode($result);
    }
    function error($mag){
        $data = [
            'error' => 1,
            'message' => $mag
        ];
        return json_encode($data);
    }
    /**
     *获取所有课程列表
     */
    public function fetchCourseList()
    {
        $Course = M('Course');
        $Course->select();
    }
    /**
     *获取单个课程列表
     * 以title（课程名称）为查询条件
     */
    public function fetchCourseId()
    {
        $Course = M('Course');
        $data['title']=$_POST['title'];
        $Course->where($data)->select();
    }
    /**
     * 添加课程数据
     */
    public function createCourse()
    {
        $Course = M('Course');
        $data['title']=I('post.title');
        $data['introduction']=I('post.introduction');
        if(empty(I('post.title'))){
            die($this->error("课程名称不能为空"));
        }
        if(empty(I('post.introduction'))){
            die ($this->error("课程简介不能为空"));
        }
        if($Course->where(array('title'=>I('post.title')))->select()){
            die($this->error("课程名称已存在"));
        }else{
            $Course->data($data)->add();
        }
        echo  $this->success();
    }

    /**
     * 修改课程信息
     */

    public function updateCourse()
    {
        $Course = M('Course');

        $data['title']=I('post.title');
        $data['introduction']=I('post.introduction');
        if(empty(I('post.title'))){
            die($this->error("课程名称不能为空"));
        }
        if(empty(I('post.introduction'))){
            die ($this->error("课程简介不能为空"));
        }
        $Course->where(array('id'=>I('get.id')))->save($data);
        echo  $this->success();
    }
}
